<template>
  <div class="training-record-list">
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="Home" :replace="true">首页</el-breadcrumb-item>
        <el-breadcrumb-item>培训记录列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" :model="searchParams">
      <el-form-item label="应聘者姓名" prop="candidateName" v-if="findCandidateFormItem">
        <el-input v-model="searchParams.candidateName" placeholder="请选择应聘者姓名" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findCandidate"></i>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch" v-if="findCandidateFormItem">查询</el-button>
        <el-button @click="onReset" v-if="findCandidateFormItem">重置</el-button>
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" @click="onExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="pageData.list"
      border
      v-loading="pageData.loading"
      size="mini"
      header-cell-class-name="table-header-cell-font"
      cell-class-name="table-cell-font">
      <el-table-column prop="candidateName" label="应聘者姓名"></el-table-column>
      <el-table-column prop="trainingName" label="培训名称"></el-table-column>
      <el-table-column prop="trainingDate" label="培训日期"></el-table-column>
      <el-table-column prop="durationHours" label="培训时长"></el-table-column>
      <el-table-column prop="provider" label="培训供应商"></el-table-column>
      <el-table-column label="培训内容">
        <template v-slot="{ row }">
          <div class="ellipsis">
            {{ row.trainingDescription }}
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="{ row }">
          <el-button @click.native.prevent="editRow(row.id)" type="primary">编辑</el-button>
          <el-button @click.native.prevent="delRow(row.id)" type="danger" plain>删除</el-button>
          <el-button @click.native.prevent="detailRow(row.id)" type="primary" plain>详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination">
      <el-pagination
        @current-change="paginationChange"
        :current-page="pageData.current"
        :page-sizes="pageData.pageSizes"
        :page-size="pageData.size"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageData.count">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="candidateSelectorVisible" v-if="candidateSelectorVisible" title="应聘者基本信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <candidate-selector @candidate-selected-event="handleCandidateSelectedEvent">
      </candidate-selector>
    </el-dialog>
    <el-dialog :visible.sync="trainingRecordAddVisible" v-if="trainingRecordAddVisible" :title="trainingRecordAddTitle" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <training-record-add :id="selectedTrainingRecordId" @close-training-record-add-event="handleCloseTrainingRecordAddEvent" @reset-training-record-add-event="handleResetTrainingRecordAddEvent">
      </training-record-add>
    </el-dialog>
    <el-dialog :visible.sync="trainingRecordViewVisible" v-if="trainingRecordViewVisible" title="培训记录详情" :top="dialogTop" :width="dialogWidth">
      <training-record-view :id="selectedTrainingRecordId" @close-training-record-view-event="handleCloseTrainingRecordViewEvent">
      </training-record-view>
    </el-dialog>
  </div>
</template>

<script>
import trainingRecordApi from '@/api/trainingRecordApi'
import CandidateSelector from "@/views/candidate/CandidateSelector.vue";
import TrainingRecordAdd from "@/views/trainingRecord/TrainingRecordAdd.vue"
import TrainingRecordView from "@/views/trainingRecord/TrainingRecordView.vue"
import listQueryMixin from '@/mixins/listQueryMixin'

export default {
  name: 'TrainingRecordList',
  components: {CandidateSelector,TrainingRecordAdd, TrainingRecordView},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        candidateId: '',
        candidateName: '',
      },
      candidateSelectorVisible: false,
      trainingRecordAddVisible: false,
      trainingRecordAddTitle: '',
      trainingRecordViewVisible: false,
      selectedTrainingRecordId: 0,
      selectedEducationId: 0,
      findCandidateFormItem: true
    }
  },
  created () {
    const type = sessionStorage.getItem('type')
    if (type && Number(type) === 3) {
      this.searchParams.candidateId = 2
      this.findCandidateFormItem = false
    }
  },
  methods: {
    getPageData (p) {
      return trainingRecordApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        candidateId: '',
        candidateName: '',
      }
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    onAdd () {
      this.selectedTrainingRecordId = ''
      this.trainingRecordAddVisible = true
      this.trainingRecordAddTitle = '培训记录新增'
    },
    onExport () {
      const headers = ['应聘者姓名','培训名称','培训日期','培训时长','培训供应商','培训内容']
      const params = Object.assign(this.getPaginationParams(), this.searchParams)
      this.getPageData(params).then(data => {
        if (!data || !data.data || data.data.list.length < 1) {
          this.$message.error('无数据导出')
          return
        }
        const exportData = []
        for (const d of data.data.list) {
          exportData.push([d.candidateName,d.trainingName,d.trainingDate,d.durationHours,d.provider,d.trainingDescription])
        }
        this.exportToExcel(headers, exportData)
      })
    },
    editRow (id) {
      this.selectedTrainingRecordId = id
      this.trainingRecordAddVisible = true
      this.trainingRecordAddTitle = '培训记录编辑'
    },
    delRow (id) {
      if (!id) {
        return
      }
      this.$confirm('确定要删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        trainingRecordApi.del(id)
          .then((resp) => {
            if (resp && resp.code === 1) {
              this.$message.success('删除成功!')
              this.onSearch()
            } else {
              this.$message.error('删除失败:' + (resp.msg ? resp.msg : '原因未知'))
            }
          })
          .catch(error => {
            console.error('操作失败:', error)
          })
      }).catch(() => {})
    },
    detailRow (id) {
      this.selectedTrainingRecordId = id
      this.trainingRecordViewVisible = true
    },
    findCandidate () {
      this.candidateSelectorVisible = true
    },
    handleCandidateSelectedEvent (selectedCandidate) {
      this.candidateSelectorVisible = false
      if (selectedCandidate && 'candidateId' in selectedCandidate && this.searchParams.candidateId !== selectedCandidate['candidateId']) {
        this.searchParams.candidateId = selectedCandidate['candidateId']
        this.searchParams.candidateName = selectedCandidate['candidateName']
      }
    },
    handleCloseTrainingRecordViewEvent () {
      this.trainingRecordViewVisible = false
    },
    handleResetTrainingRecordAddEvent () {
      this.trainingRecordAddTitle = '培训记录新增'
    },
    handleCloseTrainingRecordAddEvent (params) {
      if (params && params.search) {
        this.onSearch()
      }
      this.trainingRecordAddVisible = false
    }
  }
}
</script>

<style lang="scss">
</style>